package com.aio.news.utils;

import java.sql.Date;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @description: 字符串处理工具类
 * @version 1.0
 * @created 2014-1-8 下午4:47:26
 * @changeRecord
 */
public class StringUtil {

	public static boolean stringInArrayOrNot(String content, String[] array) {
		boolean result = false;
		for (String ss : array) {
			if (ss.equals(content)) {
				result = true;
				break;
			}
		}
		return result;
	}

	/* 时间戳转换成字符窜 */
	public static String getDateToString(String time) {
		try {
			Date d = new Date(Long.parseLong(time) * 1000);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			return sf.format(d);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/* 时间戳转换成字符窜 */
	public static String getDateToString2(String time) {
		try {
			Date d = new Date(Long.parseLong(time) * 1000);
			SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd");
			return sf.format(d);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/* 时间戳转换成字符窜 */
	public static String getDateToStringLong(long time) {
		try {
			Date d = new Date(time * 1000);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sf.setTimeZone(TimeZone.getTimeZone("GMT+0")); 
			return sf.format(d);
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 判断是否为字母
	 * 
	 * @return
	 */
	public static boolean check(String fstrData) {
		char c = fstrData.charAt(0);
		if (((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))) {
			return true;
		} else {
			return false;
		}
	}

	public static int getEnglishNum(String content) {
		int count = 0;
		int length = content.length();
		for (int i = 0; i < length; i++) {
			String s = content.substring(i, i + 1);
			if (check(s))
				count++;
		}
		return count;
	}

	/**
	 * 时间戳转换成字符窜 返回MM-DD格式
	 * */

	public static String getSimpleDateToString(String time) {
		try {
			Date d = new Date(Long.parseLong(time) * 1000);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			String s = sf.format(d);
			if (s != null) {
				s = s.substring(s.indexOf("-") + 1);
			}
			return s;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 保留小数点后两位
	 * 
	 * @param price
	 * @return
	 */
	public static String getPriceString(String price) {
		try {
			double d = Double.parseDouble(price);
			DecimalFormat decimal = new DecimalFormat("#.00");
			String str = decimal.format(d);
			return str;
		} catch (Exception e) {
			e.printStackTrace();
			return price;
		}
	}

	public static boolean isNullOrEmpty(String s) {
		return s == null || s.equals("");
	}

	public static String getDate(Long date) {
		String time = null;
		Long duration = Math.abs(System.currentTimeMillis() / 1000 - date);
		if (60 > duration) {
			time = duration + "초전";
		} else if (duration < 60 * 60) {
			time = duration / 60 + "분전";
		} else if (duration < 24 * 60 * 60) {
			time = duration / (60 * 60) + "시간전";
		} else if (duration < 30 * 24 * 60 * 60) {
			time = duration / (24 * 60 * 60) + "일전";
		} else {
			time = duration / (30 * 24 * 60 * 60) + "달전";
		}
		return time;
	}

	/**
	 * 验证是否为邮箱格式
	 * 
	 * @param strEmail
	 * @return
	 */
	public static boolean isEmail(String strEmail) {
		String strPattern = "^[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\\w\\.-]*[a-zA-Z0-9]\\.[a-zA-Z][a-zA-Z\\.]*[a-zA-Z]$";

		Pattern p = Pattern.compile(strPattern);
		Matcher m = p.matcher(strEmail);
		return m.matches();
	}

	// 判断手机格式是否正确
	public static boolean isMobileNO(String mobiles) {
		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(18[0-9]))\\d{8}$");
		Matcher m = p.matcher(mobiles);

		return m.matches();
	}
}
